/*
 * @Author: 刘润泽
 * @Date: 2022-11-08 18:34:08
 * @LastEditors: 刘润泽
 * @LastEditTime: 2023-03-14 15:40:32
 * @Description: 
 */
package server.business.controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import server.business.pojo.AutologousBloodInformation;
import server.business.service.AutologousBloodInformationService;
import server.business.utils.results.lrz.Result;

/**
 * @author simba@onlying.cn
 * @date 2022/9/30 16:44
 */

@RestController
@RequestMapping("/autologousBlood")
public class AutologousBloodInformationController {

    // 引入自体血信息相关功能
    @Autowired
    AutologousBloodInformationService autologousBloodInformationService;

    /**
     * 分页查询
     * 
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize,
            AutologousBloodInformation autologousBloodInformation) {
        IPage<AutologousBloodInformation> page = autologousBloodInformationService.getPage(currentPage, pageSize,
                autologousBloodInformation);
        // 如果当前页码值大于了总页码值，就重新执行查询操作，使用最大页码值作为当前页码值
        if (currentPage > page.getPages()) {
            page = autologousBloodInformationService.getPage((int) page.getPages(), pageSize,
                    autologousBloodInformation);
        }
        return Result.success(page);
    }


    /**
     * 更改血液信息
     */
    @RequestMapping(value = "/normal/updateBloodInfo", method = RequestMethod.PUT)
    @ResponseBody
    public Result updateBloodInfo(@RequestBody AutologousBloodInformation autologousBloodInformation){
        autologousBloodInformationService.updateBloodInfo(autologousBloodInformation);
        Result result = new Result();
        result.setCode("200");
        result.setMsg("自体血液信息更改成功测试");
        return result;
    }


    /**
     * 根据血液id更改血液状态
     */
    @RequestMapping(value = "/normal/setAutoBloodInfoState", method = RequestMethod.PUT)
    public Result setAutoBloodInfoState(@RequestBody AutologousBloodInformation autologousBloodInformation) {
        String bloodId = autologousBloodInformation.getBloodId();
        String state = autologousBloodInformation.getState();
        autologousBloodInformationService.setStateByBloodId(bloodId, state);
        return Result.success("自体血信息状态更改成功");
    }

    /**
     * 根据自体输血表信息获取符合条件的血液
     * @return
     */
    @GetMapping("/normal/getAutoBloodInfo")
    public Result getAutologousBloodInfoByTransfusion(AutologousBloodInformation autologousBloodInformation) {
        // 首先根据血型找到符合条件的血液
        AutologousBloodInformation data = autologousBloodInformationService
                .getAutologousBloodInfoByTransfusionAuto(autologousBloodInformation);
        // 返回符合条件的血液信息
        return Result.success(data);
    }

    /**
     * 根据编号批量打印
     * 
     * @param ids
     * @return
     */
    @GetMapping(value = "/normal/batchDownload")
    @ResponseBody
    public void batchDownload(HttpServletResponse response, @RequestParam ArrayList<Long> ids) throws IOException {
        autologousBloodInformationService.batchDownload(response, ids);
    }
}
